#include <VirtualWire.h>

volatile unsigned long steeringValue = 511;
volatile unsigned long speedValue = 511;

// RF Received Message (char and unsigned int)
char ReceivedMessage[8]; 
volatile unsigned long ReceviedValue = 0;

void setup()
{
    Serial.begin(9600);    

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true);    // Required for RX Link Module
    vw_setup(2000);                   // Bits per sec
    vw_set_rx_pin(23);           // We will be receiving on pin 23 (Mega) ie the RX pin from the module connects to this pin. 
    vw_rx_start();                      // Start the receiver 
}

void loop()
{
  uint8_t buflen = sizeof(ReceviedValue);

  if (vw_get_message((uint8_t *)&ReceviedValue, &buflen)) // check to see if anything has been received
  {    
    Serial.print("Recieved Value: ");
    Serial.print(ReceviedValue);
    Serial.println();
    
    steeringValue = ((ReceviedValue>>10)&0x3FF);
    speedValue = ReceviedValue&0x3FF;
    
    // Aciona saídas digitais
    
    if(ReceviedValue & 0x80000000) // Chave de partida
    {
      Serial.print("Chave de partida ON!");
      Serial.println();
    }
    else
    {
      Serial.print("Chave de partida OFF!");
      Serial.println();
    }
    if(ReceviedValue & 0x40000000) // Farol Baixo
    {
      Serial.print("Farol baixo ON!");
      Serial.println();
    }
    else
    {
      Serial.print("Farol baixo OFF!");
      Serial.println();
    }
    
    if(ReceviedValue & 0x40000000) // Farol Automático
    {
      Serial.print("Farol automatico ON!");
      Serial.println();
    }
    else
    {
      Serial.print("Farol automatico OFF!");
      Serial.println();
    }
    
    // DEBUG
    Serial.print("Steering Value: ");
    Serial.print(steeringValue);
    Serial.println();
    Serial.print("Speed Value: ");
    Serial.print(speedValue);
    Serial.println();
    // END DEBUG
   }
   else
   {
     Serial.print("No data received!");
     Serial.println();
   }
   delay(1500);
}

